package org.example.swagger.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import org.example.swagger.domain.User;
import org.springframework.web.bind.annotation.*;

/**
 * 用户管理接口
 *
 * @author huhao
 */
@RestController
@RequestMapping("/users")
public class UserController {

    @Operation(summary = "获取用户信息接口", description = "通过用户ID获取用户信息")
    @ApiResponses(value = {
            @ApiResponse(
                    responseCode = "200",
                    description = "用户信息",
                    content = {
                            @Content(
                                    mediaType = "application/json",
                                    schema = @Schema(implementation = User.class)
                            )
                    }
            ),
            @ApiResponse(responseCode = "404", description = "无法获取用户信息")
    })
    @GetMapping("/{id}")
    public User getUserById(@Parameter(description = "用户ID") @PathVariable("id") Long id) {
        // 模拟数据库获取用户
        User user = new User();
        user.setId(id);
        user.setName("张三");
        user.setEmail("zhangsan@example.com");
        return user;
    }

    @Operation(summary = "创建用户接口", description = "创建一个新用户并返回用户ID")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "用户创建",
                    content = {@Content(
                            mediaType = "application/json",
                            schema = @Schema(implementation = User.class)
                    )}
            )
    })
    @PostMapping
    public User createUser(@RequestBody User user) {
        // 模拟数据库保存用户并返回ID
        Long id = System.currentTimeMillis();
        user.setId(id);
        return user;
    }
}
